﻿Imports System.IO.IsolatedStorage

Public Class SaveGameDetails
    Public CurrentLevel As Integer
    Public LevelScores() As Long

    Public Sub New()
        LevelScores = New Long(11) {}
        CurrentLevel = 1
    End Sub
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        LevelScores = Nothing
    End Sub
End Class

Public Class savegame
    Private mData As SaveGameDetails

    Public Function LevelUnLocked(ByVal Level As Integer) As Boolean
        Return Level <= mData.CurrentLevel
    End Function

    Public Sub New()
        LoadData()
    End Sub

    Public Sub SetLevel(ByVal Level As Integer)
        mData.CurrentLevel = Level
    End Sub
    Public Sub SetScore(ByVal Level As Integer, ByVal Score As Long)
        If (Score > mData.LevelScores(Level - 1)) Then
            mData.LevelScores(Level - 1) = Score
        End If
    End Sub


    Private Sub LoadData()
        mData = Nothing
        mData = New SaveGameDetails

        Dim store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
        Dim stream As IsolatedStorageFileStream = New IsolatedStorageFileStream("gamedata.txt", IO.FileMode.OpenOrCreate, store)
        If (Not stream Is Nothing) Then
            If (stream.Length > 0) Then
                Dim data() As Byte = New Byte(4) {}
                Dim intStart As Integer = 4
                stream.Read(data, 0, 4)
                mData.CurrentLevel = System.BitConverter.ToInt32(data, 0)
                For intLevel As Integer = 0 To mData.LevelScores.Count - 1
                    data = Nothing
                    data = New Byte(8) {}
                    stream.Read(data, 0, 8)
                    mData.LevelScores(intLevel) = System.BitConverter.ToInt64(data, 0)
                    intStart += 8
                Next
            End If
            stream.Close() : stream.Dispose() : stream = Nothing
        End If
        store.Dispose() : store = Nothing
    End Sub
    Public Sub SaveData()
        If (mData Is Nothing) Then
            Return
        End If
        Dim store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
        store.DeleteFile("gamedata.txt")
        Dim stream As IsolatedStorageFileStream = New IsolatedStorageFileStream("gamedata.txt", IO.FileMode.OpenOrCreate, store)
        If (Not stream Is Nothing) Then
            Dim data() As Byte = System.BitConverter.GetBytes(mData.CurrentLevel)
            stream.Write(data, 0, data.Length)
            For intLevel As Integer = 0 To mData.LevelScores.Count - 1
                data = Nothing
                data = System.BitConverter.GetBytes(mData.LevelScores(intLevel))
                stream.Write(data, 0, data.Length)
            Next
            data = Nothing
            stream.Close() : stream.Dispose() : stream = Nothing
        End If
        store.Dispose() : store = Nothing
    End Sub
End Class
